package com.ryt.tuangou.service.impl;

import com.ryt.tuangou.mapper.UserMapper;
import com.ryt.tuangou.pojo.User;
import com.ryt.tuangou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServicelmpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean saveUser(User user){
        //判断username是否已存在
        User count = userMapper.getByUserName(user.getUserName());
        if(count!=null){
            //已存在
            throw new RuntimeException("username已存在");
        }
        //保存用户信息
        user.setCreateTime(LocalDateTime.now().toString());
        user.setUpdateTime(LocalDateTime.now().toString());
        return userMapper.saveUser(user);
    }
    //用户登录
    @Override
    public Boolean loginIn(User user){
        User userDb = userMapper.getByUserName(user.getUserName());
        if(user.getUserPwd().equals(userDb.getUserPwd())){
            return true;
    }
        throw new RuntimeException("用户名或密码错误");
    }

}
